<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book python高级 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>property属性 | python高级</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../day12/03.magic.html" />
    
    
    <link rel="prev" href="../day12/01.mro.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="2.2"
        data-chapter-title="property属性"
        data-filepath="day12/02.property.md"
        data-basepath=".."
        data-revision="Wed Sep 26 2018 22:46:13 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="day11/00.readme.html">
            
                
                    <a href="../day11/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Python语法高级专题1
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="day11/01.GIL.html">
            
                
                    <a href="../day11/01.GIL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        GIL问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="day11/02.copy.html">
            
                
                    <a href="../day11/02.copy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        赋值-浅拷贝-深拷贝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="day11/03.import.html">
            
                
                    <a href="../day11/03.import.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        import导入模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="day11/04.review.html">
            
                
                    <a href="../day11/04.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="day12/00.readme.html">
            
                
                    <a href="../day12/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        Python语法高级专题2
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="day12/01.mro.html">
            
                
                    <a href="../day12/01.mro.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        多继承和MRO顺序
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2.2" data-path="day12/02.property.html">
            
                
                    <a href="../day12/02.property.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        property属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="day12/03.magic.html">
            
                
                    <a href="../day12/03.magic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        魔法属性和方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="day12/04.with.html">
            
                
                    <a href="../day12/04.with.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        with管理上下文
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="day12/05.review.html">
            
                
                    <a href="../day12/05.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="day13/00.readme.html">
            
                
                    <a href="../day13/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Python语法之装饰器
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="day13/01.closure.html">
            
                
                    <a href="../day13/01.closure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        闭包
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="day13/02.decorator.html">
            
                
                    <a href="../day13/02.decorator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        装饰器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="day13/03.review.html">
            
                
                    <a href="../day13/03.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="day14/00.readme.html">
            
                
                    <a href="../day14/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        MySQL数据库基本操作
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="day14/01.数据库概述.html">
            
                
                    <a href="../day14/01.数据库概述.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        数据库概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="day14/02.数据类型和约束.html">
            
                
                    <a href="../day14/02.数据类型和约束.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        数据完整性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="day14/03.Navicat.html">
            
                
                    <a href="../day14/03.Navicat.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        图形化客户端Navicat
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="day14/04.命令行客户端.html">
            
                
                    <a href="../day14/04.命令行客户端.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        命令行客户端SQL的使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.4.1" data-path="day14/05.sql_con.html">
            
                
                    <a href="../day14/05.sql_con.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.1.</b>
                        
                        登录和退出数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.2" data-path="day14/07.sql_table_struct.html">
            
                
                    <a href="../day14/07.sql_table_struct.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.2.</b>
                        
                        表结构的操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.3" data-path="day14/06.sql_database.html">
            
                
                    <a href="../day14/06.sql_database.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.3.</b>
                        
                        数据库的操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.4" data-path="day14/08.表数据操作.html">
            
                
                    <a href="../day14/08.表数据操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.4.</b>
                        
                        表数据的操作
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="day14/09.review.html">
            
                
                    <a href="../day14/09.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        知识总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="day15/00.readme.html">
            
                
                    <a href="../day15/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        MYSQL数据库进阶操作
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="day15/01.练习.html">
            
                
                    <a href="../day15/01.练习.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        练习强化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="day15/02.where条件.html">
            
                
                    <a href="../day15/02.where条件.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        条件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="day15/03.排序查询.html">
            
                
                    <a href="../day15/03.排序查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="day15/04.分页查询.html">
            
                
                    <a href="../day15/04.分页查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="day15/05.聚合函数.html">
            
                
                    <a href="../day15/05.聚合函数.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        聚合函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="day15/06.分组.html">
            
                
                    <a href="../day15/06.分组.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        分组
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="day15/07.连接.html">
            
                
                    <a href="../day15/07.连接.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="day15/08.自连接.html">
            
                
                    <a href="../day15/08.自连接.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        自连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.9" data-path="day15/09.子查询.html">
            
                
                    <a href="../day15/09.子查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                        子查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.10" data-path="day15/10.总结.html">
            
                
                    <a href="../day15/10.总结.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.10.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="day16/readme.html">
            
                
                    <a href="../day16/readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        MYSQL数据库编程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="day16/data.html">
            
                
                    <a href="../day16/data.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        数据准备
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="day16/sql_code.html">
            
                
                    <a href="../day16/sql_code.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        SQL演练
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="day16/dbdesign.html">
            
                
                    <a href="../day16/dbdesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        数据库设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="day16/pymysql.html">
            
                
                    <a href="../day16/pymysql.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        数据库编程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="day16/zongjie.html">
            
                
                    <a href="../day16/zongjie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="day17/00.readme.html">
            
                
                    <a href="../day17/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        MySQL数据库高级用法
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="day17/01.view.html">
            
                
                    <a href="../day17/01.view.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="day17/02.transaction.html">
            
                
                    <a href="../day17/02.transaction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        事务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="day17/03.sql_index.html">
            
                
                    <a href="../day17/03.sql_index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        索引
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="day17/04.user.html">
            
                
                    <a href="../day17/04.user.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        用户管理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="day17/05.master_slave.html">
            
                
                    <a href="../day17/05.master_slave.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        主从配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.6" data-path="day17/06.review.html">
            
                
                    <a href="../day17/06.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.6.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="day18/00.readme.html">
            
                
                    <a href="../day18/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        miniweb框架
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="day18/01.框架是什么.html">
            
                
                    <a href="../day18/01.框架是什么.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        框架概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="day18/02.实现框架.html">
            
                
                    <a href="../day18/02.实现框架.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        实现一个简单的框架
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="day18/03.模板.html">
            
                
                    <a href="../day18/03.模板.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        模板替换
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="day18/04.路由.html">
            
                
                    <a href="../day18/04.路由.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        路由概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="day18/05.显示股票数据.html">
            
                
                    <a href="../day18/05.显示股票数据.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        股票收藏夹
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="day18/05.review.html">
            
                
                    <a href="../day18/05.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        复习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >python高级</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="3-property&#x5C5E;&#x6027;">3 property&#x5C5E;&#x6027;</h1>
<h2 id="31-&#x5B66;&#x4E60;&#x76EE;&#x6807;">3.1 &#x5B66;&#x4E60;&#x76EE;&#x6807;</h2>
<ul>
<li>&#x80FD;&#x591F;&#x8BF4;&#x51FA;&#x4F7F;&#x7528;property&#x7684;&#x597D;&#x5904;</li>
</ul>
<h2 id="32-property&#x5C5E;&#x6027;">3.2 property&#x5C5E;&#x6027;</h2>
<p>&#x4E00;&#x79CD;&#x7528;&#x8D77;&#x6765;&#x50CF;&#x662F;&#x4F7F;&#x7528;&#x7684;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#x4E00;&#x6837;&#x7684;&#x7279;&#x6B8A;&#x5C5E;&#x6027;&#xFF0C;&#x53EF;&#x4EE5;&#x5BF9;&#x5E94;&#x4E8E;&#x67D0;&#x4E2A;&#x65B9;&#x6CD5;</p>
<pre><code class="lang-python"><span class="hljs-comment"># ############### &#x5B9A;&#x4E49; ###############</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">pass</span>

    <span class="hljs-comment"># &#x5B9A;&#x4E49;property&#x5C5E;&#x6027;</span>
<span class="hljs-decorator">    @property</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">prop</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">pass</span>

<span class="hljs-comment"># ############### &#x8C03;&#x7528; ###############</span>
foo_obj = Foo()
foo_obj.func()  <span class="hljs-comment"># &#x8C03;&#x7528;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;</span>
foo_obj.prop  <span class="hljs-comment"># &#x8C03;&#x7528;property&#x5C5E;&#x6027;</span>
</code></pre>
<p><img src="../Images/new_13day/QQ20171025-211026@2x.png" alt=""></p>
<blockquote>
<p>property&#x5C5E;&#x6027;&#x7684;&#x5B9A;&#x4E49;&#x548C;&#x8C03;&#x7528;&#x8981;&#x6CE8;&#x610F;&#x4E00;&#x4E0B;&#x51E0;&#x70B9;&#xFF1A;</p>
</blockquote>
<ul>
<li>&#x5B9A;&#x4E49;&#x65F6;&#xFF0C;&#x5728;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x7684;&#x57FA;&#x7840;&#x4E0A;&#x6DFB;&#x52A0; @property &#x88C5;&#x9970;&#x5668;&#xFF1B;&#x5E76;&#x4E14;&#x4EC5;&#x6709;&#x4E00;&#x4E2A;self&#x53C2;&#x6570;</li>
<li>&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x65E0;&#x9700;&#x62EC;&#x53F7;<pre><code class="lang-python">  &#x65B9;&#x6CD5;&#xFF1A;foo_obj.func()
  property&#x5C5E;&#x6027;&#xFF1A;foo_obj.prop
</code></pre>
</li>
</ul>
<h2 id="33-&#x7B80;&#x5355;&#x7684;&#x5B9E;&#x4F8B;">3.3 &#x7B80;&#x5355;&#x7684;&#x5B9E;&#x4F8B;</h2>
<p>&#x5BF9;&#x4E8E;&#x4EAC;&#x4E1C;&#x5546;&#x57CE;&#x4E2D;&#x663E;&#x793A;&#x7535;&#x8111;&#x4E3B;&#x673A;&#x7684;&#x5217;&#x8868;&#x9875;&#x9762;&#xFF0C;&#x6BCF;&#x6B21;&#x8BF7;&#x6C42;&#x4E0D;&#x53EF;&#x80FD;&#x628A;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x5185;&#x5BB9;&#x90FD;&#x663E;&#x793A;&#x5230;&#x9875;&#x9762;&#x4E0A;&#xFF0C;&#x800C;&#x662F;&#x901A;&#x8FC7;&#x5206;&#x9875;&#x7684;&#x529F;&#x80FD;&#x5C40;&#x90E8;&#x663E;&#x793A;&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x5411;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x65F6;&#x5C31;&#x8981;&#x663E;&#x793A;&#x7684;&#x6307;&#x5B9A;&#x83B7;&#x53D6;&#x4ECE;&#x7B2C;m&#x6761;&#x5230;&#x7B2C;n&#x6761;&#x7684;&#x6240;&#x6709;&#x6570;&#x636E;</p>
<blockquote>
<p>&#x8FD9;&#x4E2A;&#x5206;&#x9875;&#x7684;&#x529F;&#x80FD;&#x5305;&#x62EC;&#xFF1A;</p>
<ul>
<li><p>&#x6839;&#x636E;&#x7528;&#x6237;&#x8BF7;&#x6C42;&#x7684;&#x5F53;&#x524D;&#x9875;&#x548C;&#x603B;&#x6570;&#x636E;&#x6761;&#x6570;&#x8BA1;&#x7B97;&#x51FA; m &#x548C; n</p>
</li>
<li><p>&#x6839;&#x636E;m &#x548C; n &#x53BB;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x8BF7;&#x6C42;&#x6570;&#x636E; </p>
</li>
</ul>
</blockquote>
<pre><code class="lang-python"><span class="hljs-comment"># ############### &#x5B9A;&#x4E49; ###############</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Pager</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, current_page)</span>:</span>
        <span class="hljs-comment"># &#x7528;&#x6237;&#x5F53;&#x524D;&#x8BF7;&#x6C42;&#x7684;&#x9875;&#x7801;&#xFF08;&#x7B2C;&#x4E00;&#x9875;&#x3001;&#x7B2C;&#x4E8C;&#x9875;...&#xFF09;</span>
        self.current_page = current_page
        <span class="hljs-comment"># &#x6BCF;&#x9875;&#x9ED8;&#x8BA4;&#x663E;&#x793A;10&#x6761;&#x6570;&#x636E;</span>
        self.per_items = <span class="hljs-number">10</span> 

<span class="hljs-decorator">    @property</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start</span><span class="hljs-params">(self)</span>:</span>
        val = (self.current_page - <span class="hljs-number">1</span>) * self.per_items
        <span class="hljs-keyword">return</span> val

<span class="hljs-decorator">    @property</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">end</span><span class="hljs-params">(self)</span>:</span>
        val = self.current_page * self.per_items
        <span class="hljs-keyword">return</span> val

<span class="hljs-comment"># ############### &#x8C03;&#x7528; ###############</span>
p = Pager(<span class="hljs-number">1</span>)
p.start  <span class="hljs-comment"># &#x5C31;&#x662F;&#x8D77;&#x59CB;&#x503C;&#xFF0C;&#x5373;&#xFF1A;m</span>
p.end  <span class="hljs-comment"># &#x5C31;&#x662F;&#x7ED3;&#x675F;&#x503C;&#xFF0C;&#x5373;&#xFF1A;n</span>
</code></pre>
<p>&#x4ECE;&#x4E0A;&#x8FF0;&#x53EF;&#x89C1;&#xFF0C;</p>
<ul>
<li>Python&#x7684;property&#x5C5E;&#x6027;&#x7684;&#x529F;&#x80FD;&#x662F;&#xFF1A;property&#x5C5E;&#x6027;&#x5185;&#x90E8;&#x8FDB;&#x884C;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x903B;&#x8F91;&#x8BA1;&#x7B97;&#xFF0C;&#x6700;&#x7EC8;&#x5C06;&#x8BA1;&#x7B97;&#x7ED3;&#x679C;&#x8FD4;&#x56DE;&#x3002;</li>
</ul>
<p>&#x7531;&#x6B64;&#x53EF;&#x89C1;&#xFF0C;<strong>property&#x7684;&#x4F5C;&#x7528;&#x5C31;&#x662F; &#x5C06;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#x7684;&#x64CD;&#x4F5C;&#x65B9;&#x6CD5;&#x5C01;&#x88C5;&#x4E3A;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#xFF0C;&#x7528;&#x6237;&#x7528;&#x8D77;&#x6765;&#x5C31;&#x548C;&#x64CD;&#x4F5C;&#x666E;&#x901A;&#x5C5E;&#x6027;&#x5B8C;&#x5168;&#x4E00;&#x81F4;,&#x975E;&#x5E38;&#x7B80;&#x5355;&#x3002;</strong></p>
<h2 id="34-property&#x5C5E;&#x6027;&#x7684;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;">3.4 property&#x5C5E;&#x6027;&#x7684;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;</h2>
<ul>
<li>&#x88C5;&#x9970;&#x5668; &#x5373;&#xFF1A;&#x5728;&#x65B9;&#x6CD5;&#x4E0A;&#x5E94;&#x7528;&#x88C5;&#x9970;&#x5668;</li>
<li>&#x7C7B;&#x5C5E;&#x6027; &#x5373;&#xFF1A;&#x5728;&#x7C7B;&#x4E2D;&#x5B9A;&#x4E49;&#x503C;&#x4E3A;property&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x5C5E;&#x6027;</li>
</ul>
<h3 id="341-&#x88C5;&#x9970;&#x5668;&#x65B9;&#x5F0F;">3.4.1 &#x88C5;&#x9970;&#x5668;&#x65B9;&#x5F0F;</h3>
<p>&#x5728;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x4E0A;&#x5E94;&#x7528;@property&#x88C5;&#x9970;&#x5668;</p>
<p>Python&#x4E2D;&#x7684;&#x7C7B;&#x6709;<code>&#x7ECF;&#x5178;&#x7C7B;</code>&#x548C;<code>&#x65B0;&#x5F0F;&#x7C7B;</code>&#xFF0C;<code>&#x65B0;&#x5F0F;&#x7C7B;</code>&#x7684;&#x5C5E;&#x6027;&#x6BD4;<code>&#x7ECF;&#x5178;&#x7C7B;</code>&#x7684;&#x5C5E;&#x6027;&#x4E30;&#x5BCC;&#x3002;&#xFF08; &#x5982;&#x679C;&#x7C7B;&#x7EE7;object&#xFF0C;&#x90A3;&#x4E48;&#x8BE5;&#x7C7B;&#x662F;&#x65B0;&#x5F0F;&#x7C7B; &#xFF0C;Python3&#x4E2D;&#x9ED8;&#x8BA4;&#x6240;&#x6709;&#x7C7B;&#x4E3A;&#x65B0;&#x5F0F;&#x7C7B;&#xFF09;</p>
<h4 id="3411-&#x7ECF;&#x5178;&#x7C7B;&#xFF0C;&#x5177;&#x6709;&#x4E00;&#x79CD;property&#x88C5;&#x9970;&#x5668;">3.4.1.1 &#x7ECF;&#x5178;&#x7C7B;&#xFF0C;&#x5177;&#x6709;&#x4E00;&#x79CD;@property&#x88C5;&#x9970;&#x5668;</h4>
<pre><code class="lang-python"><span class="hljs-comment"># ############### &#x5B9A;&#x4E49; ###############    </span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Goods</span>:</span>
<span class="hljs-decorator">    @property</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">price</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;laowang&quot;</span>
<span class="hljs-comment"># ############### &#x8C03;&#x7528; ###############</span>
obj = Goods()
result = obj.price  <span class="hljs-comment"># &#x81EA;&#x52A8;&#x6267;&#x884C; @property &#x4FEE;&#x9970;&#x7684; price &#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x83B7;&#x53D6;&#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x503C;</span>
print(result)
</code></pre>
<h4 id="3412-&#x65B0;&#x5F0F;&#x7C7B;&#xFF0C;&#x5177;&#x6709;&#x4E09;&#x79CD;property&#x88C5;&#x9970;&#x5668;">3.4.1.2 &#x65B0;&#x5F0F;&#x7C7B;&#xFF0C;&#x5177;&#x6709;&#x4E09;&#x79CD;@property&#x88C5;&#x9970;&#x5668;</h4>
<pre><code class="lang-python"><span class="hljs-comment">#coding=utf-8</span>
<span class="hljs-comment"># ############### &#x5B9A;&#x4E49; ###############</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Goods</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;python3&#x4E2D;&#x9ED8;&#x8BA4;&#x7EE7;&#x627F;object&#x7C7B;
        &#x4EE5;python2&#x3001;3&#x6267;&#x884C;&#x6B64;&#x7A0B;&#x5E8F;&#x7684;&#x7ED3;&#x679C;&#x4E0D;&#x540C;&#xFF0C;&#x56E0;&#x4E3A;&#x53EA;&#x6709;&#x5728;python3&#x4E2D;&#x624D;&#x6709;@xxx.setter  @xxx.deleter
    &quot;&quot;&quot;</span>
<span class="hljs-decorator">    @property</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">price</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&apos;@property&apos;</span>)

<span class="hljs-decorator">    @price.setter</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">price</span><span class="hljs-params">(self, value)</span>:</span>
        print(<span class="hljs-string">&apos;@price.setter&apos;</span>)

<span class="hljs-decorator">    @price.deleter</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">price</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&apos;@price.deleter&apos;</span>)

<span class="hljs-comment"># ############### &#x8C03;&#x7528; ###############</span>
obj = Goods()
obj.price          <span class="hljs-comment"># &#x81EA;&#x52A8;&#x6267;&#x884C; @property &#x4FEE;&#x9970;&#x7684; price &#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x83B7;&#x53D6;&#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x503C;</span>
obj.price = <span class="hljs-number">123</span>    <span class="hljs-comment"># &#x81EA;&#x52A8;&#x6267;&#x884C; @price.setter &#x4FEE;&#x9970;&#x7684; price &#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x5C06;  123 &#x8D4B;&#x503C;&#x7ED9;&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;</span>
<span class="hljs-keyword">del</span> obj.price      <span class="hljs-comment"># &#x81EA;&#x52A8;&#x6267;&#x884C; @price.deleter &#x4FEE;&#x9970;&#x7684; price &#x65B9;&#x6CD5;</span>
</code></pre>
<h4 id="3413-&#x6CE8;&#x610F;">3.4.1.3 &#x6CE8;&#x610F;</h4>
<ul>
<li>&#x7ECF;&#x5178;&#x7C7B;&#x4E2D;&#x7684;&#x5C5E;&#x6027;&#x53EA;&#x6709;&#x4E00;&#x79CD;&#x8BBF;&#x95EE;&#x65B9;&#x5F0F;&#xFF0C;&#x5176;&#x5BF9;&#x5E94;&#x88AB; @property &#x4FEE;&#x9970;&#x7684;&#x65B9;&#x6CD5;</li>
<li>&#x65B0;&#x5F0F;&#x7C7B;&#x4E2D;&#x7684;&#x5C5E;&#x6027;&#x6709;&#x4E09;&#x79CD;&#x8BBF;&#x95EE;&#x65B9;&#x5F0F;&#xFF0C;&#x5E76;&#x5206;&#x522B;&#x5BF9;&#x5E94;&#x4E86;&#x4E09;&#x4E2A;&#x88AB;@property&#x3001;@&#x65B9;&#x6CD5;&#x540D;.setter&#x3001;@&#x65B9;&#x6CD5;&#x540D;.deleter&#x4FEE;&#x9970;&#x7684;&#x65B9;&#x6CD5;</li>
</ul>
<p>&#x7531;&#x4E8E;&#x65B0;&#x5F0F;&#x7C7B;&#x4E2D;&#x5177;&#x6709;&#x4E09;&#x79CD;&#x8BBF;&#x95EE;&#x65B9;&#x5F0F;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x5B83;&#x4EEC;&#x51E0;&#x4E2A;&#x5C5E;&#x6027;&#x7684;&#x8BBF;&#x95EE;&#x7279;&#x70B9;&#xFF0C;&#x5206;&#x522B;&#x5C06;&#x4E09;&#x4E2A;&#x65B9;&#x6CD5;&#x5B9A;&#x4E49;&#x4E3A;&#x5BF9;&#x540C;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#xFF1A;&#x83B7;&#x53D6;&#x3001;&#x4FEE;&#x6539;&#x3001;&#x5220;&#x9664;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Goods</span><span class="hljs-params">(object)</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-comment"># &#x539F;&#x4EF7;</span>
        self.original_price = <span class="hljs-number">100</span>
        <span class="hljs-comment"># &#x6298;&#x6263;</span>
        self.discount = <span class="hljs-number">0.8</span>

<span class="hljs-decorator">    @property</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">price</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-comment"># &#x5B9E;&#x9645;&#x4EF7;&#x683C; = &#x539F;&#x4EF7; * &#x6298;&#x6263;</span>
        new_price = self.original_price * self.discount
        <span class="hljs-keyword">return</span> new_price

<span class="hljs-decorator">    @price.setter</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">price</span><span class="hljs-params">(self, value)</span>:</span>
        self.original_price = value

<span class="hljs-decorator">    @price.deleter</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">price</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">del</span> self.original_price

obj = Goods()
obj.price         <span class="hljs-comment"># &#x83B7;&#x53D6;&#x5546;&#x54C1;&#x4EF7;&#x683C;</span>
obj.price = <span class="hljs-number">200</span>   <span class="hljs-comment"># &#x4FEE;&#x6539;&#x5546;&#x54C1;&#x539F;&#x4EF7;</span>
<span class="hljs-keyword">del</span> obj.price     <span class="hljs-comment"># &#x5220;&#x9664;&#x5546;&#x54C1;&#x539F;&#x4EF7;</span>
</code></pre>
<h4 id="342-&#x7C7B;&#x5C5E;&#x6027;&#x65B9;&#x5F0F;&#xFF0C;&#x521B;&#x5EFA;&#x503C;&#x4E3A;property&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x5C5E;&#x6027;">3.4.2 &#x7C7B;&#x5C5E;&#x6027;&#x65B9;&#x5F0F;&#xFF0C;&#x521B;&#x5EFA;&#x503C;&#x4E3A;property&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x5C5E;&#x6027;</h4>
<ul>
<li>&#x5F53;&#x4F7F;&#x7528;&#x7C7B;&#x5C5E;&#x6027;&#x7684;&#x65B9;&#x5F0F;&#x521B;&#x5EFA;property&#x5C5E;&#x6027;&#x65F6;&#xFF0C;<code>&#x7ECF;&#x5178;&#x7C7B;</code>&#x548C;<code>&#x65B0;&#x5F0F;&#x7C7B;</code>&#x65E0;&#x533A;&#x522B;</li>
</ul>
<pre><code class="lang-python">
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_bar</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;laotie&apos;</span>

    BAR = property(get_bar)

obj = Foo()
reuslt = obj.BAR  <span class="hljs-comment"># &#x81EA;&#x52A8;&#x8C03;&#x7528;get_bar&#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x83B7;&#x53D6;&#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x503C;</span>
print(reuslt)
</code></pre>
<p><strong>&gt; property&#x65B9;&#x6CD5;&#x4E2D;&#x6709;&#x4E2A;&#x56DB;&#x4E2A;&#x53C2;&#x6570;
*</strong> &#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x65B9;&#x6CD5;&#x540D;&#xFF0C;&#x8C03;&#x7528; &#x5BF9;&#x8C61;.&#x5C5E;&#x6027; &#x65F6;&#x81EA;&#x52A8;&#x89E6;&#x53D1;&#x6267;&#x884C;&#x65B9;&#x6CD5;</p>
<ul>
<li>&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x65B9;&#x6CD5;&#x540D;&#xFF0C;&#x8C03;&#x7528; &#x5BF9;&#x8C61;.&#x5C5E;&#x6027; &#xFF1D; XXX &#x65F6;&#x81EA;&#x52A8;&#x89E6;&#x53D1;&#x6267;&#x884C;&#x65B9;&#x6CD5;</li>
<li>&#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x65B9;&#x6CD5;&#x540D;&#xFF0C;&#x8C03;&#x7528; del &#x5BF9;&#x8C61;.&#x5C5E;&#x6027; &#x65F6;&#x81EA;&#x52A8;&#x89E6;&#x53D1;&#x6267;&#x884C;&#x65B9;&#x6CD5;</li>
<li>&#x7B2C;&#x56DB;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8C03;&#x7528; &#x5BF9;&#x8C61;.&#x5C5E;&#x6027;.__doc__ &#xFF0C;&#x6B64;&#x53C2;&#x6570;&#x662F;&#x8BE5;&#x5C5E;&#x6027;&#x7684;&#x63CF;&#x8FF0;&#x4FE1;&#x606F;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment">#coding=utf-8</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_bar</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&quot;getter...&quot;</span>)
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;laowang&apos;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">set_bar</span><span class="hljs-params">(self, value)</span>:</span> 
        <span class="hljs-string">&quot;&quot;&quot;&#x5FC5;&#x987B;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&quot;&quot;&quot;</span>
        print(<span class="hljs-string">&quot;setter...&quot;</span>)
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;set value&apos;</span> + value

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">del_bar</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&quot;deleter...&quot;</span>)
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;laowang&apos;</span>

    BAR = property(get_bar, set_bar, del_bar, <span class="hljs-string">&quot;description...&quot;</span>)

obj = Foo()

obj.BAR  <span class="hljs-comment"># &#x81EA;&#x52A8;&#x8C03;&#x7528;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;get_bar</span>
obj.BAR = <span class="hljs-string">&quot;alex&quot;</span>  <span class="hljs-comment"># &#x81EA;&#x52A8;&#x8C03;&#x7528;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;set_bar&#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x5C06;&#x201C;alex&#x201D;&#x5F53;&#x4F5C;&#x53C2;&#x6570;&#x4F20;&#x5165;</span>
desc = Foo.BAR.__doc__  <span class="hljs-comment"># &#x81EA;&#x52A8;&#x83B7;&#x53D6;&#x7B2C;&#x56DB;&#x4E2A;&#x53C2;&#x6570;&#x4E2D;&#x8BBE;&#x7F6E;&#x7684;&#x503C;&#xFF1A;description...</span>
print(desc)
<span class="hljs-keyword">del</span> obj.BAR  <span class="hljs-comment"># &#x81EA;&#x52A8;&#x8C03;&#x7528;&#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;del_bar&#x65B9;&#x6CD5;</span>
</code></pre>
<p>&#x7531;&#x4E8E;<code>&#x7C7B;&#x5C5E;&#x6027;&#x65B9;&#x5F0F;</code>&#x521B;&#x5EFA;property&#x5C5E;&#x6027;&#x5177;&#x6709;3&#x79CD;&#x8BBF;&#x95EE;&#x65B9;&#x5F0F;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x5B83;&#x4EEC;&#x51E0;&#x4E2A;&#x5C5E;&#x6027;&#x7684;&#x8BBF;&#x95EE;&#x7279;&#x70B9;&#xFF0C;&#x5206;&#x522B;&#x5C06;&#x4E09;&#x4E2A;&#x65B9;&#x6CD5;&#x5B9A;&#x4E49;&#x4E3A;&#x5BF9;&#x540C;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#xFF1A;&#x83B7;&#x53D6;&#x3001;&#x4FEE;&#x6539;&#x3001;&#x5220;&#x9664;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Goods</span><span class="hljs-params">(object)</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-comment"># &#x539F;&#x4EF7;</span>
        self.original_price = <span class="hljs-number">100</span>
        <span class="hljs-comment"># &#x6298;&#x6263;</span>
        self.discount = <span class="hljs-number">0.8</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_price</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-comment"># &#x5B9E;&#x9645;&#x4EF7;&#x683C; = &#x539F;&#x4EF7; * &#x6298;&#x6263;</span>
        new_price = self.original_price * self.discount
        <span class="hljs-keyword">return</span> new_price

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">set_price</span><span class="hljs-params">(self, value)</span>:</span>
        self.original_price = value

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">del_price</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">del</span> self.original_price

    PRICE = property(get_price, set_price, del_price, <span class="hljs-string">&apos;&#x4EF7;&#x683C;&#x5C5E;&#x6027;&#x63CF;&#x8FF0;...&apos;</span>)

obj = Goods()
obj.PRICE         <span class="hljs-comment"># &#x83B7;&#x53D6;&#x5546;&#x54C1;&#x4EF7;&#x683C;</span>
obj.PRICE = <span class="hljs-number">200</span>   <span class="hljs-comment"># &#x4FEE;&#x6539;&#x5546;&#x54C1;&#x539F;&#x4EF7;</span>
<span class="hljs-keyword">del</span> obj.PRICE     <span class="hljs-comment"># &#x5220;&#x9664;&#x5546;&#x54C1;&#x539F;&#x4EF7;</span>
</code></pre>
<h2 id="&#x603B;&#x7ED3;">&#x603B;&#x7ED3;</h2>
<ul>
<li>&#x5B9A;&#x4E49;property&#x5C5E;&#x6027;&#x5171;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x5206;&#x522B;&#x662F;&#x3010;&#x88C5;&#x9970;&#x5668;&#x3011;&#x548C;&#x3010;&#x7C7B;&#x5C5E;&#x6027;&#x3011;&#xFF0C;&#x800C;&#x3010;&#x88C5;&#x9970;&#x5668;&#x3011;&#x65B9;&#x5F0F;&#x9488;&#x5BF9;&#x7ECF;&#x5178;&#x7C7B;&#x548C;&#x65B0;&#x5F0F;&#x7C7B;&#x53C8;&#x6709;&#x6240;&#x4E0D;&#x540C;&#x3002;</li>
<li>&#x901A;&#x8FC7;&#x4F7F;&#x7528;property&#x5C5E;&#x6027;&#xFF0C;&#x80FD;&#x591F;&#x7B80;&#x5316;&#x8C03;&#x7528;&#x8005;&#x5728;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x7684;&#x6D41;&#x7A0B;</li>
<li>x = property(get_x, set_x, del_x, &quot;doc&quot;)</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../day12/01.mro.html" class="navigation navigation-prev " aria-label="Previous page: 多继承和MRO顺序"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../day12/03.magic.html" class="navigation navigation-next " aria-label="Next page: 魔法属性和方法"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book python高级 -->
</html>
